package com.hzlj.position.locate.controller;


import cn.hutool.core.date.DateUtil;
import com.fz.common.base.dto.PageParamDTO;
import com.fz.common.base.dto.QueryPageResultDTO;
import com.fz.common.base.dto.ResultDTO;
import com.fz.common.base.office.EasyExcelUtil;
import com.hzlj.position.locate.common.dto.abnMissingJzry.AbnMissingJzryQueryParamDTO;
import com.hzlj.position.locate.common.dto.abnMissingJzry.AbnMissingJzryQueryResultDTO;
import com.hzlj.position.locate.common.dto.abnMissingJzryNotice.AbnMissingJzryNoticeQueryResultDTO;
import com.hzlj.position.locate.common.enums.AbnMissingCheckStatus;
import com.hzlj.position.locate.common.enums.AbnMissingStep;
import com.hzlj.position.locate.service.AbnMissingJzryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 【定位分析-疑似失联人员】接口
 *
 * @author makejava
 * @date 2024-06-25 14:40:44
 */
@Slf4j
@RestController
@RequestMapping("/position/abn/missing/jzry")
public class AbnMissingJzryController {

    @Resource
    private AbnMissingJzryService abnMissingJzryService;

    /**
     * 通过id查询定位分析-疑似失联人员
     */
    @GetMapping("/{id}")
    public ResultDTO<AbnMissingJzryQueryResultDTO> getAbnMissingJzryById(@PathVariable("id") String id) {
        log.debug("【查询信息】定位分析-疑似失联人员:id={}", id);
        return new ResultDTO<>(abnMissingJzryService.getAbnMissingJzryById(id));
    }


    /**
     * 手动分析
     */
    @GetMapping("/abn")
    public ResultDTO<?> abn() {
        abnMissingJzryService.abn();
        return new ResultDTO<>();
    }


    /**
     * 导出 定位分析-疑似失联人员
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response,
                       AbnMissingJzryQueryParamDTO dto) {
        dto.setWithNotice(true);
        List<AbnMissingJzryQueryResultDTO> list = abnMissingJzryService.listAbnMissingJzry(dto);
        String fileName = "疑似失联人员";
        EasyExcelUtil.downCustomerExcel(response, fileName, fileName,
                EasyExcelUtil.Field.<AbnMissingJzryQueryResultDTO>of()
                        .add(AbnMissingJzryQueryResultDTO::getXm, "姓名")
                        .add(AbnMissingJzryQueryResultDTO::getJgMc, "机构")
                        .add(e -> DateUtil.formatDateTime(e.getStartTime()) + "~" + DateUtil.formatDateTime(e.getEndTime()), "定位记录")
                        .add(e -> "24小时未产生定位记录", "详情")
                        .add(e -> {
                            AbnMissingJzryNoticeQueryResultDTO notice = e.notice(AbnMissingStep.STEP1);
                            return notice == null ? "" : DateUtil.formatDateTime(notice.getNoticeTime());
                        }, "电话通知矫正对象时间")
                        .add(e -> {
                            AbnMissingJzryNoticeQueryResultDTO notice = e.notice(AbnMissingStep.STEP1);
                            return notice == null ? "" : notice.getNoticeMsg();
                        }, "详情")
                        .add(e -> {
                            if (AbnMissingCheckStatus.JZRY_REPORT.getStatus().equals(e.getMissingCheckStatus())) {
                                return DateUtil.formatDateTime(e.getReportTime());
                            }
                            return "";
                        }, "矫正对象上报时间")
                        .add(e -> {
                            if (AbnMissingCheckStatus.JZRY_REPORT.getStatus().equals(e.getMissingCheckStatus())) {
                                AbnMissingJzryNoticeQueryResultDTO notice = e.notice(AbnMissingStep.STEP3);
                                if (notice != null && notice.getExt() != null && notice.getExt().getPosition() != null) {
                                    return notice.getExt().getPosition().getPosition();
                                }
                            }
                            return "";
                        }, "详情")
                        .add(e -> {
                            AbnMissingJzryNoticeQueryResultDTO notice = e.notice(AbnMissingStep.STEP2);
                            return notice == null ? "" : DateUtil.formatDateTime(notice.getNoticeTime());
                        }, "电话通知紧急联系人时间")
                        .add(e -> {
                            AbnMissingJzryNoticeQueryResultDTO notice = e.notice(AbnMissingStep.STEP2);
                            return notice == null ? "" : notice.getNoticeMsg();
                        }, "详情")
                        .add(e -> {
                            AbnMissingJzryNoticeQueryResultDTO notice = e.notice(AbnMissingStep.STEP3);
                            return notice == null ? "" : DateUtil.formatDateTime(notice.getNoticeTime());
                        }, "结果通知工作人员时间")
                        .add(e -> {
                            AbnMissingJzryNoticeQueryResultDTO notice = e.notice(AbnMissingStep.STEP3);
                            return notice == null ? "" : notice.getNoticeMsg();
                        }, "详情")
                        .getFields(),
                list
        );
    }

    /**
     * 列表查询 定位分析-疑似失联人员
     */
    @GetMapping("/list")
    public ResultDTO<List<AbnMissingJzryQueryResultDTO>> listAbnMissingJzry(AbnMissingJzryQueryParamDTO dto) {
        log.debug("【查询无分页列表】信息定位分析-疑似失联人员,AbnMissingJzryQueryParamDTO={}", dto);
        return new ResultDTO<>(abnMissingJzryService.listAbnMissingJzry(dto));
    }

    /**
     * 分页查询 定位分析-疑似失联人员
     */
    @GetMapping("/list/page")
    public ResultDTO<QueryPageResultDTO<AbnMissingJzryQueryResultDTO>> pageAbnMissingJzry(AbnMissingJzryQueryParamDTO dto, PageParamDTO pageParamDTO) {
        log.debug("【查询分页列表】信息定位分析-疑似失联人员,AbnMissingJzryQueryParamDTO={},pageParamDTO={}", dto, pageParamDTO);
        return new ResultDTO<>(abnMissingJzryService.pageAbnMissingJzry(dto, pageParamDTO));
    }

}

